/*
 * PanelTowerSettingsHomeBase.java
 *
 * Created on May 29, 2008, 7:27 PM
 */
package hextd.tower;

import hextd.creep.Factory;
import hextd.core.ContextNormal;
import hextd.util.ParameterUtils;
import hextd.util.SiFormatter;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;

/**
 *
 * @author  hylke
 */
public class PanelTowerSettingsHomeBase extends PanelTowerSettings {

	private TowerHome t;
	private ContextNormal c;
	private boolean loading = false;
	//
	private String[] creeps;
	private int selectedCreep = 0;
	//
	private String[] sizes;
	private int selectedSize = 0;
	//
	private String[] paths;
	private int selectedPath = 0;
	//
	private String[] healths;
	private int selectedHealth = 0;
	//
	private SpinnerNumberModel creepCountSpinner;

	public PanelTowerSettingsHomeBase(TowerHome t, ContextNormal c) {
		this.creepCountSpinner = new SpinnerNumberModel(1, 0, 10, 1);
		this.c = c;
		initComponents();
		this.t = t;
	}

	public void doSendCreeps() {
		t.trySendCreeps();
	}

	public void nextMode(int m, int step) {
		if (!SwingUtilities.isEventDispatchThread()) {
			final int m2 = m;
			final int step2 = step;
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					nextMode(m2, step2);
				}
			});
		} else {
			if (m == 1) {
				selectedCreep += step;
				selectedCreep = ParameterUtils.forceIndexBounds(creeps, selectedCreep);
				jComboBox_creepType.setSelectedIndex(selectedCreep);
			} else if (m == 2) {
				selectedSize += step;
				selectedSize = ParameterUtils.forceIndexBounds(sizes, selectedSize);
				jComboBox_creepSizes.setSelectedIndex(selectedSize);
			} else if (m == 3) {
				int oldCount = Integer.parseInt(jSpinner_creepCount.getValue().toString());
				jSpinner_creepCount.setValue(oldCount + step);
			} else if (m == 4) {
				selectedPath += step;
				selectedPath = ParameterUtils.forceIndexBounds(paths, selectedPath);
				jComboBox_paths.setSelectedIndex(selectedPath);
			} else if (m == 5) {
				selectedHealth += step;
				selectedHealth = ParameterUtils.forceIndexBounds(healths, selectedHealth);
				jComboBox_health.setSelectedIndex(selectedHealth);
			}
		}
	}

	@Override
	public void setMode(int m, int v, boolean suppress) {
		if (!SwingUtilities.isEventDispatchThread()) {
			final int m2 = m;
			final int v2 = v;
			final Boolean s2 = suppress;
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					setMode(m2, v2, s2);
				}
			});
		} else {
			this.loading = suppress;
			if (m == 1) {
				this.selectedCreep = v;
				this.jComboBox_creepType.setSelectedIndex(v);
			} else if (m == 2) {
				this.selectedSize = v;
				this.jComboBox_creepSizes.setSelectedIndex(v);
			} else if (m == 3) {
				this.jSpinner_creepCount.setValue(v);
			} else if (m == 4) {
				this.selectedPath = v;
				this.jComboBox_paths.setSelectedIndex(v);
			} else if (m == 5) {
				this.selectedHealth = v;
				this.jComboBox_health.setSelectedIndex(v);
			}
			this.loading = false;
		}
	}

	@Override
	public void setModes(int m, String[] modes) {
		if (!SwingUtilities.isEventDispatchThread()) {
			final int m2 = m;
			final String[] modes2 = modes;
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					setModes(m2, modes2);
				}
			});

		} else {
			if (m == 1) {
				this.loading = true;
				this.creeps = modes;
				this.jComboBox_creepType.removeAllItems();
				for (int i = 0; i < this.creeps.length; i++) {
					this.jComboBox_creepType.addItem(Factory.identifyEnemy(this.creeps[i]).toString());
				}
				this.jComboBox_creepType.setSelectedIndex(this.selectedCreep);
				//this.jLabel_mode1.setText(this.modes1[this.mode1]);
				this.loading = false;
			} else if (m == 2) {
				this.loading = true;
				this.sizes = modes;
				this.jComboBox_creepSizes.removeAllItems();
				for (int i = 0; i < this.sizes.length; i++) {
					this.jComboBox_creepSizes.addItem(this.sizes[i]);
				}
				if (this.selectedSize > this.sizes.length - 1) {
					this.selectedSize = this.sizes.length - 1;
				}
				this.jComboBox_creepSizes.setSelectedIndex(this.selectedSize);
				//this.jLabel_mode1.setText(this.modes1[this.mode1]);
				this.loading = false;
			} else if (m == 3) {
				int max = new Integer(modes[0]);
				int cur = new Integer(this.jSpinner_creepCount.getValue().toString());
				this.creepCountSpinner.setMaximum(max);
				if (cur > max) {
					this.creepCountSpinner.setValue(max);
				}
			} else if (m == 4) {
				this.loading = true;
				this.paths = modes;
				this.jComboBox_paths.removeAllItems();
				for (int i = 0; i < this.paths.length; i++) {
					this.jComboBox_paths.addItem(this.paths[i]);
				}
				if (this.selectedPath < 0 || this.selectedPath >= this.paths.length) {
					this.selectedPath = 0;
				}
				if (this.paths.length > 0) {
					this.jComboBox_paths.setSelectedIndex(this.selectedPath);
				}
				//this.jLabel_mode1.setText(this.modes1[this.mode1]);
				this.loading = false;
			} else if (m == 5) {
				this.loading = true;
				this.healths = modes;
				this.jComboBox_health.removeAllItems();
				for (int i = 0; i < this.healths.length; i++) {
					this.jComboBox_health.addItem(this.healths[i]);
				}
				if (this.selectedHealth < 0 || this.selectedHealth >= this.healths.length) {
					this.selectedHealth = 0;
				}
				if (this.healths.length > 0) {
					this.jComboBox_health.setSelectedIndex(this.selectedHealth);
				}
				this.loading = false;
			}
		}
	}

	@Override
	public int getMode(int m) {
		if (m == 1) {
			return this.selectedCreep;
		} else if (m == 2) {
			return this.selectedSize;
		} else if (m == 3) {
			return new Integer(this.jSpinner_creepCount.getValue().toString());
		} else if (m == 4) {
			return this.selectedPath;
		} else if (m == 5) {
			return this.selectedHealth;
		} else {
			return -1;
		}
	}

	@Override
	public void setEnabled(boolean enable) {
		if (!SwingUtilities.isEventDispatchThread()) {
			final boolean localEnable = enable;
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					setEnabled(localEnable);
				}
			});
		} else {
			this.jButton_sendCreeps.setEnabled(enable);
			this.jComboBox_creepSizes.setEnabled(enable);
			this.jComboBox_creepType.setEnabled(enable);
			this.jSpinner_creepCount.setEnabled(enable);
			this.jComboBox_paths.setEnabled(enable);
			this.jComboBox_health.setEnabled(enable);
		}
	}

	private void updatePrice() {
		if (!SwingUtilities.isEventDispatchThread()) {
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					updatePrice();
				}
			});
		} else {
			long costs = t.getCosts();
			jLabel1.setText("Total: " + SiFormatter.format(costs) + "€");
		}
	}

	@Override
	public void setCanLock(boolean b) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jComboBox_creepType = new javax.swing.JComboBox();
        jComboBox_creepSizes = new javax.swing.JComboBox();
        jSpinner_creepCount = new javax.swing.JSpinner();
        jButton_sendCreeps = new javax.swing.JButton();
        jComboBox_paths = new javax.swing.JComboBox();
        jComboBox_health = new javax.swing.JComboBox();
        jLabel_hp = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        jComboBox_creepType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox_creepType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_creepTypeActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0E-4;
        add(jComboBox_creepType, gridBagConstraints);

        jComboBox_creepSizes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "N", "S", "L" }));
        jComboBox_creepSizes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_creepSizesActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jComboBox_creepSizes, gridBagConstraints);

        jSpinner_creepCount.setModel(this.creepCountSpinner);
        jSpinner_creepCount.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner_creepCountStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        add(jSpinner_creepCount, gridBagConstraints);

        jButton_sendCreeps.setText("Buy");
        jButton_sendCreeps.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_sendCreepsActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0E-4;
        add(jButton_sendCreeps, gridBagConstraints);

        jComboBox_paths.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox_paths.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_pathsActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        add(jComboBox_paths, gridBagConstraints);

        jComboBox_health.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox_health.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_healthActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0E-4;
        add(jComboBox_health, gridBagConstraints);

        jLabel_hp.setBackground(c.colors.getColorBackGround());
        jLabel_hp.setFont(jLabel_hp.getFont());
        jLabel_hp.setForeground(c.colors.getColorForeGround());
        jLabel_hp.setText("health");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 4);
        add(jLabel_hp, gridBagConstraints);

        jLabel1.setBackground(c.colors.getColorBackGround());
        jLabel1.setForeground(c.colors.getColorForeGround());
        jLabel1.setText("Total:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 2, 2, 2);
        add(jLabel1, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    private void jComboBox_creepTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_creepTypeActionPerformed
		if (!this.loading) {
			this.selectedCreep = this.jComboBox_creepType.getSelectedIndex();
			this.t.runMode(1, this.selectedCreep);
			updatePrice();
		}
    }//GEN-LAST:event_jComboBox_creepTypeActionPerformed

    private void jButton_sendCreepsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_sendCreepsActionPerformed
		this.doSendCreeps();
    }//GEN-LAST:event_jButton_sendCreepsActionPerformed

    private void jComboBox_creepSizesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_creepSizesActionPerformed
		if (!this.loading) {
			this.selectedSize = this.jComboBox_creepSizes.getSelectedIndex();
			this.t.runMode(2, this.selectedSize);
			updatePrice();
		}
    }//GEN-LAST:event_jComboBox_creepSizesActionPerformed

    private void jSpinner_creepCountStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner_creepCountStateChanged
		if (!this.loading) {
			this.t.runMode(3, new Integer(this.jSpinner_creepCount.getValue().toString()));
			updatePrice();
		}
    }//GEN-LAST:event_jSpinner_creepCountStateChanged

    private void jComboBox_pathsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_pathsActionPerformed
		if (!this.loading) {
			this.selectedPath = this.jComboBox_paths.getSelectedIndex();
			this.t.runMode(4, this.selectedPath);
		}
    }//GEN-LAST:event_jComboBox_pathsActionPerformed

private void jComboBox_healthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_healthActionPerformed
	if (!this.loading) {
		this.selectedHealth = this.jComboBox_health.getSelectedIndex();
		this.t.runMode(5, this.selectedHealth);
		updatePrice();
	}
}//GEN-LAST:event_jComboBox_healthActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_sendCreeps;
    private javax.swing.JComboBox jComboBox_creepSizes;
    private javax.swing.JComboBox jComboBox_creepType;
    private javax.swing.JComboBox jComboBox_health;
    private javax.swing.JComboBox jComboBox_paths;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel_hp;
    private javax.swing.JSpinner jSpinner_creepCount;
    // End of variables declaration//GEN-END:variables
}
